package com.readboottest.controller;

import com.alibaba.fastjson.JSONObject;
import com.readboottest.chain.ReadChain;
import com.readboottest.chain.client.SearchWebsite;
import com.readboottest.entity.Book;
import com.readboottest.entity.BookCatalogue;
import com.readboottest.entity.request.novel.ReadNovelContentRequestVo;
import com.readboottest.entity.request.novel.WebsiteRequest;
import com.readboottest.entity.response.BaseResponse;
import com.readboottest.util.BeanUtilS;
import com.readboottest.util.HttpUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import static com.readboottest.constant.GlobalConstants.SUCCESS;

/**
 * @author zhanghuiyong
 * @创建时间 2019年07月16日
 * @描述: 小说阅读模块
 **/

@RequestMapping("/rly/novel/")
@RestController
@CrossOrigin
public class NovelReadRestController {

    /**
     * 查询小说
     *
     * @return
     */
    @PostMapping("/queryNovel")
    @ResponseBody
    public BaseResponse queryNovel(@RequestBody WebsiteRequest novelRequestVo, HttpServletResponse response) {
        BaseResponse bookBaseResponse = new BaseResponse<>();
        System.out.println("开始搜索小说:" + novelRequestVo.getBookName());
        Book novel = null;
        try {
            //解决跨域请求问题
            HttpUtil.domainCross(response);
            ReadChain readChain = new ReadChain();
            SearchWebsite website = (SearchWebsite) readChain.execute(novelRequestVo.getWebSiteName());
            novel = website.getNovel(novelRequestVo.getBookName());
            bookBaseResponse.setStatus(SUCCESS);
            bookBaseResponse.setResult(novel);
            System.out.println("小说检索查看返回值," + JSONObject.toJSONString(bookBaseResponse));
        } catch (Exception e) {
            e.printStackTrace();
            bookBaseResponse.setMessage("小说检索出错" + e.getMessage());
        }
        return bookBaseResponse;
    }

    /**
     * 阅读小说内容
     *
     * @param novelRequestVo
     * @return
     */
    @PostMapping("/readNovelContent")
    @ResponseBody
    public BaseResponse readNovelContent(@RequestBody ReadNovelContentRequestVo novelRequestVo, HttpServletResponse response) {
        BaseResponse bookBaseResponse = new BaseResponse<>();
        System.out.println("开始阅读小说:" + novelRequestVo.getTitle());
        try {
            //解决跨域请求问题
            HttpUtil.domainCross(response);
            ReadChain readChain = new ReadChain();
            SearchWebsite website = (SearchWebsite) readChain.execute(novelRequestVo.getWebSiteName());

            BookCatalogue bookCatalogue = getBookCatalogue(novelRequestVo);
            bookCatalogue = website.getNovelContent(bookCatalogue);
            bookBaseResponse.setStatus(SUCCESS);
            bookBaseResponse.setResult(bookCatalogue);
            System.out.println("阅读小说内容返回值," + JSONObject.toJSONString(bookBaseResponse));
        } catch (Exception e) {
            e.printStackTrace();
            bookBaseResponse.setMessage("小说阅读出错" + e.getMessage());
        }
        return bookBaseResponse;
    }

    private BookCatalogue getBookCatalogue(ReadNovelContentRequestVo novelRequestVo) {
        BookCatalogue bookCatalogue = new BookCatalogue();
        BeanUtilS.copyObject(novelRequestVo, bookCatalogue);
        return bookCatalogue;
    }

}
